﻿using System;
using System.IO;
using System.Text;
using NUnit.Framework;

namespace MemoryStreamUnitTest
{
    [TestFixture]
    public class CMemoryStreamUnitTest
    {
        [Test]
        public void NewMemoryStreamIsEmpty()
        {
            MemoryStream ms = new MemoryStream(100);

            bool result = (ms.Length == 0);

            Assert.That( result, Is.True);
        }

        [Test]
        public void MemoryStreamContainsAddedStuff()
        {
            MemoryStream ms = new MemoryStream(100);
            UnicodeEncoding uniEncoding = new UnicodeEncoding();
            bool result;

            byte[] orgString1 = uniEncoding.GetBytes("We're testing the memory stream. ");
            byte[] orgString2 = uniEncoding.GetBytes("add some more stuff!");
            byte[] target = new byte[500];

            ms.Write(orgString1, 0, orgString1.Length);
            ms.Write(orgString2, 0, orgString2.Length);

            ms.Seek(0, SeekOrigin.Begin);

            result = (ms.Length == orgString1.Length + orgString2.Length);
            Assert.That( result , Is.True );
             
            target = new byte[ms.Length];
            int len = (int) ms.Length;
            ms.Read(target, 0, len );

            result = true;
            for (int i = 0; i < orgString1.Length; i++ )
            {
                if ( orgString1[ i ] != target[ i ] )
                    result = false;
            }

            for (int i = 0; i < orgString2.Length; i++)
            {
                if (orgString2[i] != target[i + orgString1.Length])
                {
                    result = false;
                }
            }

            Assert.That(result, Is.True);
        }






    }
}
